package com.feng.chat.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtil {
    public static String getImageSrc(String htmlCode) {
        Pattern p = Pattern.compile("<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(htmlCode);
        String quote = null;
        String src = null;
        if (m.find()) {
            quote = m.group(0);
            src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("\\s+")[0] : m.group(2);
            return src;

        }
        return "";
    }

    public static void main(String[] args) {
        String imageSrc = getImageSrc("<p data-v-md-line=\"1\"><img src=\"https://im.gzydong.club/public/media/image/note/20220730/52bf8122f0e01ad9c676fdae2ab3b1d9_200x199.jpg\" alt=\"d1332cb4908520b50a5caf5a8235e67f692621542.jpg\"><img src=\"https://im.gzydong.club/public/media/image/note/20220801/d315fc4aea4cfb2b8bfa8a0ae62cb782_0x0.jpg\" alt=\"apple.jpg\"><img src=\"https://im.gzydong.club/public/media/image/note/20220801/93f46d2807c632e556d1816615968124_735x396.png\" alt=\"image.png\"></p>\n");
        System.out.println(imageSrc);
    }
}
